package com.ebt.m.utils;

import android.content.Context;
import android.content.pm.PackageManager;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;

public class OssUtil {
    private static OssUtil instance;
    static String accessKey;
    static String screctKey;
    static String bucketName;
    static String bucketNameProposal;//计划书文件夹bucket
    public static final String PROPOSAL_ATTACHMENT_OBJECT_KEY = "proposal/";

    public static OSSClient ossService;
    public static final String ATTACHMENT_OBJECT_KEY = "cusotmerAtta/";

    //客户附件或录音文件OSS上传信息
    public String bucketNameCustomer = "ebt-agent";// 客户附件文件夹bucket
    public static final String OBJECT_KEY_CUSTOMER = "customer/";
    public static final String KEY_ATTACHMENT = "attachment";
    public static final String KEY_portrait = "portrait";
    public static final String KEY_TAPE = "tape";

    private OssUtil(Context context) {
        init(context);

    }

    public static OssUtil getInstance(Context context) {
        if (instance == null) {
            instance = new OssUtil(context.getApplicationContext());
        }
        return instance;
    }

    public void init(Context context) {
        try {
            accessKey = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA).metaData.getString("com.alibaba.app.ossak");
            screctKey = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA).metaData.getString("com.alibaba.app.osssk");
            bucketName = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA).metaData.getString("com.alibaba.app.ossbucketname");
            bucketNameProposal = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA).metaData.getString("com.alibaba.app.ossbucketname.proposal");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        // 初始化ossService
        initOssService(context);
    }

    private void initOssService(Context context) {

        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

        // 明文设置secret的方式建议只在测试时使用，更多鉴权模式请参考后面的访问控制章节
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKey, screctKey);

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

        ossService = new OSSClient(context, endpoint, credentialProvider, conf);
    }

}
